iT邦幫忙

0

自主學習30日-LeetCode 58:Length of Last Word

  • 分享至 

  • xImage
  •  

58.Length of Last Word

題目

給定一個只包含 英文字母與空白字元 ' ' 的字串 s,

請你回傳 最後一個單字的長度。

一個「單字」是由非空白字元組成的最長連續字串。

解題思路

1.忽略尾端空白(從右邊往左掃)

2.開始計算最後單字長度

直到遇到空白或字串開頭為止

3.回傳長度

為什麼從尾巴掃?
因為我們只關心最後一個單字,不需要拆整句。

https://ithelp.ithome.com.tw/upload/images/20251017/20169298AUa0WWtsxR.pnghttps://ithelp.ithome.com.tw/upload/images/20251017/201692984WIMy2eD7W.png


圖片
  熱門推薦
圖片
{{ item.channelVendor }} | {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言